package com.magicborrow.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.beans.HomePageEntity;
import com.magicborrow.beans.UserBean;
import com.magicborrow.utils.SharePreUtils;
import com.magicborrow.utils.UserTools;
import com.magicborrow.utils.VolleyTool;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.HashMap;
import java.util.Map;

public class MineActivity extends BaseActivity implements View.OnClickListener{

    private RelativeLayout mApply;
    private RelativeLayout mBorrow;
    private RelativeLayout mComment;
    private RelativeLayout mPeifu;
    private RelativeLayout mIPublish;
    private RelativeLayout mIBorrowed;
    private RelativeLayout mInstall;
    private RelativeLayout mAbout;
    private RelativeLayout mCollection;
    private RelativeLayout mWithDraw;
    private TextView applyIndex;
    private TextView borrowIndex;
    private TextView commentIndex;
    private TextView payoutIndex;
    private ImageView mAvatar;
    private UserBean.User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_mine);
        setHasTitle(false);
        initView();
        initListener();
    }

    @Override
    protected void onResume() {
        super.onResume();
        user = UserTools.getUser(this);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.head_default);
        mAvatar.setImageBitmap(bitmap);
        if(user !=null){
            Map<String,String> params = new HashMap<>();
            params.put("id",user.getId()+"");
            VolleyTool.get(Constants.HOME_PAGE, params, new VolleyTool.HTTPListener() {
                @Override
                public <T> void onResponse(T t, int requestCode) {
                    HomePageEntity entity= (HomePageEntity) t;
                    HomePageEntity.HomePageData data = entity.getData();
                    ((TextView)findViewById(R.id.intro)).setText(user.getSignature());
                    UserBean.User user = entity.getData().getUser();
                    ((TextView)findViewById(R.id.name)).setText(data.getUser().getNickname());
                    ((TextView)findViewById(R.id.intro)).setText(user.getSignature());
                    DisplayImageOptions options = new DisplayImageOptions.Builder().displayer(
                            new RoundedBitmapDisplayer(getResources().getDimensionPixelOffset(R.dimen._50dp))).build();
                    com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(
                            Constants.ADDRESS + data.getUser().getAvatar80(),
                            mAvatar, options);
                }

                @Override
                public void onErrorResponse(VolleyError error, int requestCode) {

                }
            }, Constants.REQUEST_ONE, HomePageEntity.class);
        }else{
            ((TextView)findViewById(R.id.name)).setText("");
            ((TextView)findViewById(R.id.intro)).setText("");
        }
        jiaobiao();
    }

    private void jiaobiao(){
        int apply = SharePreUtils.getInt(this, Constants.APPLY,0);
        if(apply > 0){
            applyIndex.setVisibility(View.VISIBLE);
            if(apply > 99){
                applyIndex.setText(apply+"+");
            }else{
                applyIndex.setText(apply+"");
            }
        }else{
            applyIndex.setVisibility(View.GONE);
        }
        int borrow = SharePreUtils.getInt(this, Constants.BORROW,0);
        if(borrow > 0){
            borrowIndex.setVisibility(View.VISIBLE);
            if(apply > 99){
                borrowIndex.setText(borrow+"+");
            }else{
                borrowIndex.setText(borrow+"");
            }
        }else{
            borrowIndex.setVisibility(View.GONE);
        }
        int comment = SharePreUtils.getInt(this, Constants.DONE,0);
        if(comment > 0){
            commentIndex.setVisibility(View.VISIBLE);
            if(comment > 99){
                commentIndex.setText(comment+"+");
            }else{
                commentIndex.setText(comment+"");
            }
        }else{
            commentIndex.setVisibility(View.GONE);
        }
        int payout = SharePreUtils.getInt(this, Constants.CLAIM,0);
        if(payout > 0){
            payoutIndex.setVisibility(View.VISIBLE);
            if(payout > 99){
                payoutIndex.setText(payout+"+");
            }else{
                payoutIndex.setText(payout+"");
            }
        }else{
            payoutIndex.setVisibility(View.GONE);
        }
    }

    @Override
    public void onBackPressed() {
        this.getParent().onBackPressed();
    }

    private void initView(){
        mApply = (RelativeLayout)findViewById(R.id.apply);
        mBorrow = (RelativeLayout)findViewById(R.id.borrow);
        mComment = (RelativeLayout)findViewById(R.id.comment);
        mPeifu = (RelativeLayout)findViewById(R.id.peifu);
        mIPublish = (RelativeLayout)findViewById(R.id.ipublish);
        mIBorrowed = (RelativeLayout)findViewById(R.id.iborrow);
        mInstall = (RelativeLayout)findViewById(R.id.install);
        mAbout = (RelativeLayout)findViewById(R.id.about);
        mCollection = (RelativeLayout)findViewById(R.id.rl_collection);
        mAvatar = (ImageView)findViewById(R.id.avatar);
        mWithDraw = (RelativeLayout)findViewById(R.id.rl_withdraw);
        applyIndex = (TextView)findViewById(R.id.applyIndex);
        borrowIndex = (TextView)findViewById(R.id.borrowIndex);
        commentIndex = (TextView)findViewById(R.id.commentIndex);
        payoutIndex = (TextView)findViewById(R.id.payoutIndex);
    }

    private void initListener(){
        mApply.setOnClickListener(this);
        mBorrow.setOnClickListener(this);
        mComment.setOnClickListener(this);
        mPeifu.setOnClickListener(this);
        mIPublish.setOnClickListener(this);
        mIBorrowed.setOnClickListener(this);
        mInstall.setOnClickListener(this);
        mAbout.setOnClickListener(this);
        mCollection.setOnClickListener(this);
        mAvatar.setOnClickListener(this);
        mWithDraw.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        if(null==user){
            intent.setClass(this,LonginActivity.class);
        }else{
            switch (v.getId()){
                case R.id.apply:
                    SharePreUtils.putInt(this,Constants.APPLY,0);
                    intent.setClass(this,MineApplyActivity.class);
                    break;
                case R.id.borrow:
                    SharePreUtils.putInt(this,Constants.BORROW,0);
                    intent.setClass(this,MineBorrowActivity.class);
                    break;
                case R.id.comment:
                    SharePreUtils.putInt(this,Constants.DONE,0);
                    intent.setClass(this,CommentNewActivity.class);
                    break;
                case R.id.peifu:
                    SharePreUtils.putInt(this,Constants.CLAIM,0);
                    intent.setClass(this,MinePayOutActivity.class);
                    break;
                case R.id.ipublish:
                    intent.setClass(this,IPublishedActivity.class);
                    break;
                case R.id.iborrow:
                    intent.setClass(this,IBorrowedActivity.class);
                    break;
                case R.id.install:
                    intent.setClass(this,BindMailboxActivity.class);
                    break;
                case R.id.about:
                    intent.setClass(this,AboutActivity.class);
                    break;
                case R.id.rl_collection:
                    intent.setClass(this,CollectionsActivity.class);
                    break;
                case R.id.rl_withdraw:
                    intent.setClass(this,MyAccountActivity.class);
                    break;
                case R.id.avatar:
                    intent.setClass(this,HomepageActivity.class);
                    intent.putExtra("id",user.getId());
                    intent.putExtra("mine",true);
                    break;
            }
        }
        startActivity(intent);
    }
}
